package com.snail.cloud.controller;

import com.snail.cloud.entities.Pay;
import com.snail.cloud.entities.PayDTO;
import com.snail.cloud.resp.ResultData;
import com.snail.cloud.servie.PayService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * 支付控制器，支付服务增删改查
 *
 * @author snail
 * @date 2024/9/20
 */
@Slf4j
@RestController
@RequestMapping("/pay")
public class PayController {

    @Resource
    private PayService payService;


    /**
     * 添加支付信息
     *
     * @param pay 支付
     * @return {@link ResultData }<{@link String }>
     * @author Snail
     * @date 2024/09/23
     */
    @PostMapping("/add")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        int i = payService.add(pay);
        return ResultData.success("成功添加支付记录,返回值" + i);
    }

    /**
     * 删除支付信息，按 Id 主键
     *
     * @param id 支付主键ID
     * @return {@link ResultData }<{@link Integer }>
     * @author Snail
     * @date 2024/09/20
     */
    @DeleteMapping("/delete/{id}")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        return ResultData.success(payService.delete(id));
    }

    /**
     * 更新支付信息
     *
     * @param payDTO 支付dto
     * @return {@link ResultData }<{@link String }>
     * @author Snail
     * @date 2024/09/20
     */
    @PutMapping("/update")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        int i = payService.update(payDTO);
        return ResultData.success("成功修改支付记录,返回值" + i);
    }

    /**
     * 获得支付信息，根据 Id 主键
     *
     * @param id 主键ID
     * @return {@link ResultData }<{@link Pay }>
     * @author Snail
     * @date 2024/09/20
     */
    @GetMapping("/get/{id}")
    public ResultData<Pay> getPay(@PathVariable("id") Integer id) {
        try {
            TimeUnit.SECONDS.sleep(62);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ResultData.success(payService.get(id));
    }

    /**
     * 获取所有支付信息
     *
     * @return {@link ResultData }<{@link List }<{@link Pay }>>
     * @author Snail
     * @date 2024/09/20
     */
    @GetMapping("/list")
    public ResultData<List<Pay>> listPay() {
        List<Pay> payList = payService.getAll();
        return ResultData.success(payList);
    }

    @Value("${server.port}")
    private String port;

    @GetMapping("/info")
    public String getInfoConsul(@Value("${snail.info}") String snailInfo) {
        return "snail:" + snailInfo + "\t,port:" + port;
    }
}
